package cn.damai.content.service.impl;

import cn.damai.content.mapper.DetailMapper;
import cn.damai.content.model.ItemDTO;
import cn.damai.content.model.ScheduleSeatPriceDTO;
import cn.damai.content.service.DetailService;
import cn.damai.model.Image;
import cn.damai.model.SchedulerSeat;
import cn.damai.model.SchedulerSeatPrice;
import cn.damai.util.DubboUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 详情业务实现类
 */
@Component
@Service(version = DubboUtil.VRESION,group = DubboUtil.GROUP)
public class DetailServiceImpl implements DetailService {

    @Autowired
    private DetailMapper detailMapper;

    @Override
    public ItemDTO queryInfoByItemId(Long itemId) {
        ItemDTO itemDTO = detailMapper.queryInfoByItemId(itemId);
//      空图片添加
        Image image = new Image();
        image.setImgUrl("/damai/image/failPicture.jpg");

        if(itemDTO.getImage() == null){
            itemDTO.setImage(image);
        }

        return itemDTO;
    }

    @Override
    public List<SchedulerSeatPrice> queryPrice(Long scheduleId) {
        List<ScheduleSeatPriceDTO> ssap = detailMapper.querySeatAndPrice(scheduleId);

        List<SchedulerSeatPrice> schedulerSeatPrices = new ArrayList<>();
//          由于sql数据座位存量不匹配,自定义方法重新计算赋值
        int areaLevelA = 0;
        int areaLevelB = 0;
        int areaLevelC = 0;

        for(int i = 0;i<ssap.size();i++){
            ScheduleSeatPriceDTO sspDTO = ssap.get(i);
            for(int j = 0;j<sspDTO.getSchedulerSeats().size();j++){
                SchedulerSeat ss = sspDTO.getSchedulerSeats().get(j);
                if(ss.getAreaLevel() == 1 && ss.getSaleStatus() == 0){
                    areaLevelA ++;
                }else if(ss.getAreaLevel() == 2 && ss.getSaleStatus() == 0){
                    areaLevelB ++;
                }else if(ss.getAreaLevel() == 3 && ss.getSaleStatus() == 0){
                    areaLevelC ++;
                }
            }

            if(sspDTO.getAreaLevel() == 1){
                sspDTO.setSeatNum(areaLevelA);
                schedulerSeatPrices.add(sspDTO);
            }else if(sspDTO.getAreaLevel() == 2){
                sspDTO.setSeatNum(areaLevelB);
                schedulerSeatPrices.add(sspDTO);
            }else if(sspDTO.getAreaLevel() == 3){
                sspDTO.setSeatNum(areaLevelC);
                schedulerSeatPrices.add(sspDTO);
            }
        }


//        for(int i = 0;i<ssap.getSchedulerSeats().size();i++){
//            SchedulerSeat schedulerSeat = ssap.getSchedulerSeats().get(i);
//            if(schedulerSeat.getAreaLevel() == 1 && schedulerSeat.getSaleStatus() == 0){
//                areaLevelA ++;
//            }else if (schedulerSeat.getAreaLevel() == 2 && schedulerSeat.getSaleStatus() == 0){
//                areaLevelB ++;
//            }else if(schedulerSeat.getAreaLevel() == 3 && schedulerSeat.getSaleStatus() == 0){
//                areaLevelC ++;
//            }
//        }
//
//        for(int j = 0;j<ssap.getSchedulerSeatPrices().size();j++){
//            SchedulerSeatPrice schedulerSeatPrice = ssap.getSchedulerSeatPrices().get(j);
//            if(schedulerSeatPrice.getAreaLevel() == 1){
//                schedulerSeatPrice.setSeatNum(areaLevelA);
//                schedulerSeatPrices.add(schedulerSeatPrice);
//            }else if(schedulerSeatPrice.getAreaLevel() == 2){
//                schedulerSeatPrice.setSeatNum(areaLevelB);
//                schedulerSeatPrices.add(schedulerSeatPrice);
//            }else if(schedulerSeatPrice.getAreaLevel() == 3){
//                schedulerSeatPrice.setSeatNum(areaLevelC);
//                schedulerSeatPrices.add(schedulerSeatPrice);
//            }
//        }

        return schedulerSeatPrices;
    }
}
